home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / CLASSSRC.PAK / REGHEAP.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  4KB  |  150 lines

  1. //----------------------------------------------------------------------------
  2. // Borland WinSys Library
  3. // Copyright (c) 1994, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   5.12  $
  6. //
  7. // TRegItem and TRegList members that need to be linked into user app and not
  8. // the WinSys DLL
  9. //----------------------------------------------------------------------------
  10. #include <winsys/pch.h>
  11. #include <winsys/registry.h>
  12. #include <stdio.h>   // sprintf()
  13. #include <tchar.h>
  14.  
  15. //
  16. // Initialize the data members to 0.
  17. //
  18. TRegFormatHeap::TRegFormatHeap()
  19. :
  20.   Head(0)
  21. {
  22. }
  23.  
  24. //
  25. // Walk the heap chain & delete the memory blocks
  26. //
  27. TRegFormatHeap::~TRegFormatHeap()
  28. {
  29.   while (Head) {
  30.     TBlock* next = Head->Next;
  31.     delete[] (_TCHAR*)Head;
  32.     Head = next;
  33.   }
  34. }
  35.  
  36. //
  37. // Allocate a block of memory of a given size & link it into the heap chain
  38. //
  39. _TCHAR* TRegFormatHeap::Alloc(int spaceNeeded)
  40. {
  41.   TBlock* newblock = (TBlock*) new _TCHAR[sizeof(TBlock) + spaceNeeded];
  42.   newblock->Next = Head;
  43.   Head = newblock;
  44.  
  45.   return newblock->Data;
  46. }
  47.  
  48. //
  49. // Lookup the key and return the associated value.
  50. // Returns an empty string if there is no associated value.
  51. // If the key does not exist, it returns a null pointer.
  52. //
  53. const _TCHAR* TRegList::Lookup(const _TCHAR* key, TLangId lang)
  54. {
  55.   if (key) {
  56.     for (TRegItem* regItem = Items; regItem->Key != 0; regItem++) {
  57.       if (_tcscmp(regItem->Key, key) == 0)
  58.         if (regItem->Value.Private)  // current can't test Value directly
  59.           return regItem->Value.Translate(lang);
  60.         else
  61.           return "";
  62.     }
  63.   }
  64.   return 0;
  65. }
  66.  
  67. //
  68. //
  69. // Return a reference to the locale string object associated with the key.
  70. //
  71. TLocaleString& TRegList::LookupRef(const _TCHAR* key)
  72. {
  73.   for (TRegItem* regItem = Items; regItem->Key != 0; regItem++) {
  74.     if (_tcscmp(regItem->Key, key) == 0)
  75.        return regItem->Value;
  76.   }
  77.   return TLocaleString::Null;
  78. }
  79.  
  80. //
  81. // Maximum string length for REGFORMAT w/ string arg. String is clipped if too
  82. // long.
  83. //
  84. const int MaxFormatLen = 40;
  85.  
  86. //
  87. // Register data formats for the object.
  88. //
  89. //
  90. _TCHAR* TRegItem::RegFormat(int f, int a, int t, int d, TRegFormatHeap& heap)
  91. {
  92.   // sprintf into sized auto buffer
  93.   // ints have a max of 11 digits: -2000000000. Add pad of 8 just in case
  94.   //
  95.   _TCHAR temp[11+1+11+1+11+1+11+1+8];
  96.   int len = _stprintf(temp, "%d,%d,%d,%d", f, a, t, d);
  97.  
  98.   // Copy into real heap buffer & return it
  99.   //
  100.   return _tcscpy(heap.Alloc(len + 1), temp);
  101. }
  102.  
  103. //
  104. //
  105. // Register data formats for the object.
  106. //
  107. _TCHAR* TRegItem::RegFormat(const _TCHAR* f, int a, int t, int d, TRegFormatHeap& heap)
  108. {
  109.   // sprintf into sized auto buffer
  110.   //
  111.   _TCHAR temp[MaxFormatLen+1+11+1+11+1+11+1+8];
  112.   int len = _stprintf(temp, "%.*s,%d,%d,%d", MaxFormatLen, (_TCHAR far*)f, a, t, d);
  113.  
  114.   // Copy into real heap buffer & return it
  115.   //
  116.   return _tcscpy(heap.Alloc(len + 1), temp);
  117. }
  118.  
  119. //
  120. // Register the flag.
  121. //
  122. //
  123. _TCHAR* TRegItem::RegFlags(long flags, TRegFormatHeap& heap)
  124. {
  125.   // sprintf into sized auto buffer
  126.   //
  127.   _TCHAR temp[11+1+8];
  128.   int len = _stprintf(temp, "%ld", flags);
  129.  
  130.   // Copy into real heap buffer & return it
  131.   //
  132.   return _tcscpy(heap.Alloc(len + 1), temp);
  133. }
  134.  
  135. //
  136. // Register the verb option.
  137. //
  138. //
  139. _TCHAR* TRegItem::RegVerbOpt(int mf, int sf, TRegFormatHeap& heap)
  140. {
  141.   // sprintf into sized auto buffer
  142.   //
  143.   _TCHAR temp[11+1+11+1+8];
  144.   int len = _stprintf(temp, "%d,%d", mf, sf);
  145.  
  146.   // Copy into real heap buffer & return it
  147.   //
  148.   return _tcscpy(heap.Alloc(len + 1), temp);
  149. }
  150.